(function($, doc) {
	$.init();
	var userinfo = app.getState() || {};

	$.plusReady(function() {
		doc.getElementById('remaining').innerHTML = userinfo.a1_money;
		doc.getElementById('integral').innerHTML = userinfo.a2_money;

        var width=document.documentElement.clientWidth;
    	var height=document.documentElement.clientHeight;
    	document.getElementById('account_pwd').style.width=width+'px';
    	document.getElementById('account_pwd').style.height=height+'px';
    	
		document.getElementById("do_post").addEventListener('tap', function(e) {
			//e.detail.gesture.preventDefault(); //修复iOS 8.x平台存在的bug，使用plus.nativeUI.prompt会造成输入法闪一下又没了
			var num = doc.getElementById('num').value;
			num = num * 1;
			var Info = {
				user_id: userinfo.user_id,
				num: num
			};

			if(Info.num == '' || Info.num < 100) {
				plus.nativeUI.toast('兑换金额不能小于100');
				return;
			}
			if(Info.num > userinfo.a1_money) {
				plus.nativeUI.toast('余额不足，不能兑换');
				return;
			}
			if(Info.num % 100 != 0) {
				plus.nativeUI.toast('兑换金额为100的整数倍');
				return;
			}
			
			document.getElementById('num').blur();
			document.getElementById('account_pwd').style.display="block";
			document.getElementById('main_content').style.display="block";
			document.getElementById('keyboard').style.display="block";
			document.getElementById('final_integer').innerHTML=num*6;
			var pwd_array=mui('.mui-input-row.mui-password input[type="password"]');
			mui.each(pwd_array,function(index,item){
				if(item.value!=''){
				   item.value='';
				}
			}) ;
		});


        //点击数字
			mui('#keyboard').on('tap', '.num.data_num', function() {
				var number=this.getAttribute('data-num');
				var pwd_array=mui('.mui-input-row.mui-password input[type="password"]');
				var f=false;
				var pwd='';
				mui.each(pwd_array,function(index,item){
				   if(item.value==''){
				   	   item.value=number;
				   	   return false;
				   }
				}) ;

				mui.each(pwd_array,function(index,item){
				   if(item.value==''){
				   	   f=true;
				   	   return false;
				   }
				   pwd+=item.value;
				}) ;
				
				if(!f){
					plus.nativeUI.showWaiting(); 
					var num=doc.getElementById('num').value;
					num=num*1;
					var Info={
						user_id: userinfo.user_id,
						trade_pwd: pwd,
						num: num,
						_token:userinfo.token
					};
					document.getElementById('account_pwd').style.display="none";
					document.getElementById('main_content').style.display="none";
					document.getElementById('keyboard').style.display="none";
					mui.ajax({
						url: apiurl + "Shift/do_convert/",
						data: Info,
						async: true,
						dataType: 'json',
						crossDomain: true, //强制使用5+跨域
						type: 'post',
						timeout: 10000,
						success: function(res) {
							plus.nativeUI.closeWaiting();  
							console.log(JSON.stringify(res));
							document.getElementById('final_integer').innerHTML='';
							if(res.status ==-3){
								mui.toast(res.msg);
								app.out();
								return false;
							}
							
							// 请求成功
							if(res.status == 0) {
								mui.alert(res.msg);
							} else {
								plus.nativeUI.toast(res.msg);
								doc.getElementById('remaining').innerHTML=res.data.a1_money;
				                doc.getElementById('integral').innerHTML=res.data.a2_money;
				                
								userinfo.a1_money = res.data.a1_money;
								userinfo.a2_money = res.data.a2_money;
								app.setState(userinfo);
								
								doc.getElementById('num').value='';
						        mui.fire(plus.webview.getLaunchWebview(), 'refresh_amoney');
							}
						},
						error: function(xhr, type, errorThrown) {
							//console.log(JSON.stringify(xhr));
							//console.log(JSON.stringify(type));
							//console.log(JSON.stringify(errorThrown));
							mui.toast('操作失败！');
						}
					});
				}
			   
			})
			
			
        document.getElementById('close_win').addEventListener('tap', function() {
			document.getElementById('account_pwd').style.display="none";
			document.getElementById('main_content').style.display="none";
			document.getElementById('keyboard').style.display="none";
			document.getElementById('final_integer').innerHTML='';
		})
        
        
        //点击删除
			mui('#keyboard').on('tap', '#numC', function() {
			     var pwd_array=mui('.mui-input-row.mui-password input[type="password"]');
				 var len=pwd_array.length;
				 for(i=len-1;i>=0;i--){
				 	if(pwd_array[i].value!=''){
				 		pwd_array[i].value='';
				 		break;
				 	}
				 }
			});
			
			
			
		doc.getElementById('recordofexchange').addEventListener('tap', function() {
			//打开兑换记录页
			mui.openWindow({
				id: 'recordofexchange.html',
				url: 'recordofexchange.html',
				show: {
					autoShow: false
				},
				waiting: {
					autoShow: true, //自动显示等待框，默认为true
					title: '正在加载...' //等待对话框上显示的提示内容
				}
			});
		})
	})

}(mui, document));